<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\CollectForms;
use app\model\RegisterField;

class CollectformsController extends Base
{

	function index()
	{
		$weid = weid();
		$page = input('post.page', 1, 'intval');
		$ptype = trim(input('post.ptype', '', 'serach_in'));

		$Fielddata = RegisterField::where(['weid' => $weid, 'ptype' => $ptype])->select()->toArray();

		if (empty($Fielddata)) {
			RegisterField::datainitial($ptype);
		}

		$query = $this->setSearch();

		$res = $query->order('sort asc,id desc')
			->paginate(getpage())
			->toArray();

		if (!empty($res['data'])) {
			foreach ($res['data'] as &$vo) {
				$vo = CollectForms::conversion($vo);
			}
		}
		$data['data'] = $res;
		if ($page == 1) {
			$data['field_data']['RegisterField'] = RegisterField::getlistViewField($ptype);
		}

		return $this->json($data);
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		$ptype = trim(input('post.ptype', '', 'serach_in'));
		$data = RegisterField::getinputField($ptype);

		if (!empty($id)) {
			$infodata = CollectForms::find($id)?->toArray();

			if (!empty($infodata)) {
				$customtext = iunserializer($infodata['customtext']);
			}
		}

		foreach ($data as &$vo) {

			if ($vo['is_sys'] == 1) {
				if (!empty($infodata[$vo['fieldsmingcheng']])) {
					$vo['fieldsvalue'] = $infodata[$vo['fieldsmingcheng']];
				}
				if ($vo['fieldsmingcheng'] == "sex") {
					$vo['selectvaluearray'] = sexarray();
				}
			} else {
				$vo['fieldsvalue'] = $customtext[$vo['inputtype']][$vo['id']];
				if (!empty($vo['selectvalue'])) {
					$selectvaluearray = explode(',', $vo['selectvalue']);
					if (is_array($selectvaluearray)) {
						foreach ($selectvaluearray as $key => $svo) {
							$vo['selectvaluearray'][$key]['val'] = $svo;
							$vo['selectvaluearray'][$key]['key'] = $svo;
						}
					}
				}
			}

			if ($vo['inputtype'] == 'pics') {
				$vo['fieldsvalue1'] = $vo['fieldsvalue'];
				if (empty($vo['fieldsvalue'])) {
					$vo['fieldsvalue'] = [];
				} else {
					$vo['fieldsvalue'] = setPicsView($vo['fieldsvalue']);
				}
			}

			if ($vo['inputtype'] == 'date') {
				$vo['fieldsvalue'] =  time_format($vo['fieldsvalue']);
			}
		}
		if (!empty($infodata)) {
			$retdata['id'] = $infodata['id'];
			$retdata['infodata'] = $infodata;
		}

		$retdata['fields'] = $data;

		return $this->json(['data' => $retdata]);
	}

	function setSearch()
	{
		$keyword = trim(input('post.keyword', '', 'serach_in'));
		$status = input('post.status', '', 'serach_in');
		$weid = weid();

		$query = CollectForms::where(['weid' => $weid]);

		if (!empty($this->sid)) {
			$query->where('sid', $this->sid);
		}

		if (!empty($keyword)) {

			$query->where('title|tel|customtext', 'like', '%' . $keyword . '%');
		}

		if (!empty($status) || $status === "0") {
			$query->where(['status' => $status]);
		}
		return $query;
	}
	function listUpdate()
	{
		$data = only('id,status');
		if (!$data['id']) throw new ValidateException('参数错误');
		CollectForms::update($data);

		return $this->json(['msg' => '操作成功']);
	}
	function delete()
	{
		return $this->del(new CollectForms());
	}
	//导出
	function dumpdata()
	{

		$page = $this->request->post('page', 1, 'intval');
		$ptype = 'CollectForms';
		$query = $this->setSearch();
		$count = $query->count();
		if ($count > 0) {
			$data = RegisterField::dumpdata($query, $ptype, $page);
		} else {
			throw new ValidateException('没有可导出的数据');
		}
		return $this->json($data);
	}
}
